package com.easy.cms.material.controller;
import com.easy.cms.base.CommonResult;

import com.easy.cms.material.model.EnterpriseMaterialBinding;
import com.easy.cms.material.service.EnterpriseMaterialBindingService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/enterprise-material-bindings")
public class EnterpriseMaterialBindingController {
    private final EnterpriseMaterialBindingService service;

    public EnterpriseMaterialBindingController(EnterpriseMaterialBindingService service) {
        this.service = service;
    }

    @PostMapping
    public CommonResult<EnterpriseMaterialBinding> create(@RequestBody EnterpriseMaterialBinding binding) {
        return CommonResult.success(service.save(binding));
    }

    @DeleteMapping("/{id}")
    public CommonResult<?> delete(@PathVariable Long id) {
        service.deleteById(id);
        return CommonResult.success(null);
    }

    @PutMapping
    public CommonResult<EnterpriseMaterialBinding> update(@RequestBody EnterpriseMaterialBinding binding) {
        return CommonResult.success(service.update(binding));
    }

    @GetMapping("/{id}")
    public CommonResult<EnterpriseMaterialBinding> getById(@PathVariable Long id) {
        return CommonResult.success(service.findById(id));
    }

    @GetMapping
    public CommonResult<Page<EnterpriseMaterialBinding>> search(
            @RequestParam(defaultValue = "") String materialName,
            @RequestParam(defaultValue = "") String customCode,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return CommonResult.success(service.findByMaterialNameAndCustomCode(materialName, customCode, pageable));
    }
}